# -*- coding: utf-8 -*

import os
import csv

from google.appengine.ext import webapp
from google.appengine.ext import db

import datastore

def fillStopColumns(modelClass, row, kargs):
    model = modelClass(location=db.GeoPt(row[2], row[3]))
    model.name = row[1].decode('utf-8')
    model.update_location()
    return model
    
def fillLineColumns(modelClass, row, kargs):
    model = modelClass()
    model.name = row[1].decode('utf-8')
    if row[2] == '1':
        model.type = 'bus'
    elif row[2] == '2':
        model.type = 'mrt'
    return model

def fillLineStopColumns(modelClass, row, kargs):
    model = modelClass()
    stopKeys = kargs['stopKeys']
    lineKeys = kargs['lineKeys']

    model.line = lineKeys[row[1]]
    model.stop = stopKeys[row[2]]
    model.stopNext = stopKeys[row[3]]
    return model

def fillHotSpotCategoryColumns(modelClass, row, kargs):
    model = modelClass()
    model.name = row[1].decode('utf-8')
    return model

def fillHotSpotColumns(modelClass, row, kargs):
    model = modelClass(location=db.GeoPt(row[6], row[5]))
    categoryKeys = kargs['categoryKeys']

    model.name = row[1].decode('utf-8')
    model.desc = row[2].decode('utf-8')
    model.spendMoney = int(row[3])
    model.spendTime = int(row[4])
    model.openTime = row[7]
    model.category = categoryKeys[row[8]]
    model.update_location()
    return model

def fillMrtPriceColumns(modelClass, row, kargs):
    model = modelClass()
    stopKeys = kargs['stopKeys']
    model.stopStart = stopKeys[row[1]]
    model.stopEnd = stopKeys[row[2]]
    model.price = int(row[3])
    return model
    
def readCsvToDatastore(filename, modelClass, fillColumns, **kargs):
    pathStop = os.path.join(os.path.dirname(__file__), 'data/handcrafted/' + filename)
    reader = csv.reader(open(pathStop, 'r'), delimiter=';')
    keys = {}
    for row in reader:
        model = fillColumns(modelClass, row, kargs)
        model.put()
        keys[row[0]] = model.key()
    return keys

def truncateAllData(modelClass):
    query = modelClass.all(keys_only=True)
    while True:
        if query.count() == 0:
            break
        db.delete(query.fetch(100))

class InitDataPage(webapp.RequestHandler):
    def get(self):
        truncateAllData(datastore.MrtPrice)
        truncateAllData(datastore.Stop)
        truncateAllData(datastore.Line)
        truncateAllData(datastore.LineStop)
        truncateAllData(datastore.HotSpotCategory)
        truncateAllData(datastore.HotSpot)
        

        stopKeys = readCsvToDatastore('Stop.csv', datastore.Stop,
                                      fillStopColumns)

        lineKeys = readCsvToDatastore('Line.csv', datastore.Line,
                                      fillLineColumns)

        readCsvToDatastore('LineStop.csv', datastore.LineStop,
                           fillLineStopColumns, stopKeys=stopKeys,
                           lineKeys=lineKeys)


        categoryKeys = readCsvToDatastore('HotSpotCategory.csv',
                                          datastore.HotSpotCategory,
                                          fillHotSpotCategoryColumns)

        readCsvToDatastore('HotSpot.csv', datastore.HotSpot,
                           fillHotSpotColumns, categoryKeys=categoryKeys)

        readCsvToDatastore('MrtPrice.csv', datastore.MrtPrice,
	                   fillMrtPriceColumns, stopKeys=stopKeys)

        self.response.out.write('okay')
        
